/**
 * 
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import systole.domain.clinicalInformation.Surgery;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.SurgeryBroker;
import systole.view.messages.ErrorMessages;

/**
 * @author jmj
 *
 */
public class SurgeryBrokerDB extends BrokerDB implements SurgeryBroker {

    /**
     * 
     */
    public SurgeryBrokerDB() {
        super();

    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.SurgeryBroker#getAllSurgery()
     */
    @Override
    public List<Surgery> getAllSurgery() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting all surgeries");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<Surgery> list = currentSession.createQuery("from Surgery s order by upper(s.name)").list();
            this.logger.logDebug("getting all surgeries successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all surgeries, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las Cirugías", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.SurgeryBroker#getSurgeryById(java.lang.Integer)
     */
    @Override
    public Surgery getSurgeryById(Integer id) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting surgery by id, id: " + id);
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Surgery instance = (Surgery) currentSession.get(Surgery.class, id);
            this.logger.logDebug("get surgery successfully");
            return instance;
        } catch (HibernateException e) {
            this.logger.logError("error on get surgery, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener la Cirugía", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.SurgeryBroker#insert(systole.domain.clinicalInformation.Surgery)
     */
    @Override
    public void insert(Surgery surgery) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving surgery ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(surgery);
            this.logger.logDebug("save successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on save surgery, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.SurgeryBroker#update(systole.domain.clinicalInformation.Surgery)
     */
    @Override
    public void update(Surgery surgery) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating surgery");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(surgery);
            this.logger.logDebug("update successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on update medic, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    @SuppressWarnings("unchecked")
    public boolean existSurgery(String name, Integer id) throws ExceptionDAO {
        try {
            this.logger.logDebug("exist Surgery");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Surgery.class).
                    add((id == null) ? Restrictions.eq("name", name)
                    : Restrictions.and(Restrictions.eq("name", name), Restrictions.ne("id", id)));
            List list = criteria.list();
            this.logger.logDebug("exist Surgery successfully");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist Surgery, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo verificar si ya existe la cirugía", e.fillInStackTrace());
        }
    }

    public Surgery getSurgeryByName(String name) throws ExceptionDAO {
        try {
            this.logger.logDebug("get Surgery by name");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Surgery.class).
                    add(Restrictions.eq("name", name)).setMaxResults(1);
            return (Surgery) criteria.uniqueResult();
        } catch (HibernateException e) {
            this.logger.logError("error on get Surgery by name, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener la cirugía", e.fillInStackTrace());
        }
    }
}
